package com.lc.afbx.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.WoDeZiXunAdapter;
import com.lc.afbx.conn.GetApiWoDeZiXun;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class ConsultationMineActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshListView pullToRefreshListView;
    private ListView consultation_mine_lv;
    private List<GetApiWoDeZiXun.Data> dataList = new ArrayList<>();
    private WoDeZiXunAdapter adapter;

    private GetApiWoDeZiXun getApiWoDeZiXun = new GetApiWoDeZiXun(new AsyCallBack<GetApiWoDeZiXun.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiWoDeZiXun.Info info) throws Exception {

            dataList.addAll(info.dataList);

            adapter.notifyDataSetChanged();

            setLastUpdateTime();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullDownRefreshComplete();
            pullToRefreshListView.onPullUpRefreshComplete();

        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consultation_mine);

        pullToRefreshListView = ((PullToRefreshListView) findViewById(R.id.consultation_mine_lv));
        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(false);
        pullToRefreshListView.setPullRefreshEnabled(true);

        consultation_mine_lv = pullToRefreshListView.getRefreshableView();

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                dataList.clear();

                getApiWoDeZiXun.userid = BaseApplication.BasePreferences.readUserId();
                getApiWoDeZiXun.execute(ConsultationMineActivity.this);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        consultation_mine_lv.setAdapter(adapter = new WoDeZiXunAdapter(context, dataList));

        getApiWoDeZiXun.userid = BaseApplication.BasePreferences.readUserId();
        getApiWoDeZiXun.execute(this);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;


        }


    }

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss");
        return mDateFormat.format(new Date(time));
    }

}
